/*
 * @author java_deng
 * @date 2024/12/4 11:57
 * @description
 */
package org.kaka.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.kaka.dto.LikeDto;
import org.kaka.dto.PostDto;
import org.kaka.mapper.LikeMapper;
import org.kaka.mapper.PostMapper;
import org.kaka.po.PostPo;
import org.kaka.service.PostService;
import org.kaka.utils.PageResponseData;
import org.kaka.vo.PostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private LikeMapper likeMapper;

    public PageResponseData<PostVo> list(PostPo postPo) {
        Page<PostVo> objects = PageHelper.startPage(postPo.getPageNo(), postPo.getPageSize());
        List<PostVo> list = postMapper.list(postPo);
        list.stream().forEach(item->item.setType(1));
        return new PageResponseData<>(list,objects.getTotal(),objects.getPages());
    }

    public PageResponseData<PostVo> managerList(PostPo postPo){
        Page<PostVo> objects = PageHelper.startPage(postPo.getPageNo(), postPo.getPageSize());
        List<PostVo> list = postMapper.managerList(postPo);
        return new PageResponseData<>(list,objects.getTotal(),objects.getPages());
    }

    @Override
    public void save(PostDto postDto) {
        postDto.setStatus(1);
        postMapper.insert(postDto);
    }

    @Override
    public void update(PostDto postDto) {
        postDto.setStatus(1);
        postMapper.updateById(postDto);
    }

    @Override
    public PostVo detail(Long id,Long userId) {
        PostVo detail = postMapper.detail(id);
        LikeDto likeDto=likeMapper.findByPostIdAndUserId(id,userId);
        if(likeDto!=null){
            detail.setIsLike(true);
        }else {
            detail.setIsLike(false);
        }
        detail.setType(1);
        return detail;
    }
    @Transactional
    public void like(LikeDto likeDto) {
        likeMapper.insert(likeDto);

        Long postId = likeDto.getPostId();
        PostDto postDto = new PostDto();
        PostDto postDto1 = postMapper.selectById(postId);
        postDto.setId(postId);
        postDto.setLikeCount(postDto1.getLikeCount()+1);
        postMapper.updateById(postDto);
    }

    @Transactional
    public void cancelLike(LikeDto likeDto) {
        likeMapper.deleteByPostIdAndUserId(likeDto.getPostId(), likeDto.getUserId());

        Long postId = likeDto.getPostId();
        PostDto postDto = new PostDto();
        PostDto postDto1 = postMapper.selectById(postId);
        postDto.setId(postId);
        postDto.setLikeCount(postDto1.getLikeCount()-1);
        postMapper.updateById(postDto);
    }

    public void delete(Long id) {
        postMapper.deleteById(id);
    }

    public void audit(PostPo postPo) {
        PostDto postDto = new PostDto();
        postDto.setId(postPo.getId());
        postDto.setStatus(postPo.getStatus());
        postMapper.updateById(postDto);
    }
}
